package com.edu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.dao.impl.GoodsDaoImpl;
import com.edu.entity.Goods;
import com.edu.service.impl.GoodsServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class goodsServlet
 */
@WebServlet("/goodsServlet")
public class goodsServlet extends HttpServlet {
	GoodsServiceImpl gsi = new GoodsServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id;
		if(request.getParameter("goodsId") == "" || request.getParameter("goodsId") == null) {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			HttpSession session = request.getSession();
			ObjectMapper om = new ObjectMapper();
			int pageNum = 1;
			int pageSize = 5;
			if(request.getParameter("pageNum")!=null) {
				pageNum = Integer.parseInt(request.getParameter("pageNum"));
			}
			System.out.println("当前页码："+pageNum);
			if(request.getParameter("pageSize")!=null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));
			}
			System.out.println("显示数量"+pageSize);
			String json = om.writeValueAsString(gsi.getAllGoods(pageNum, pageSize));
			PrintWriter pw = response.getWriter();
			pw.print(json);
			pw.flush();
			return;
		}else {
			id = Integer.parseInt(request.getParameter("goodsId"));
			gsi.delGoods(id);
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
